<?php 
class DB{
	var $db;
	function __construct() {
		$dsn = "mysql:dbname=mxx;host=localhost;port=3306";
		$user = "root";
		$password = "root";
		try {
		    $this->db = new PDO($dsn, $user, $password,array(PDO::ATTR_PERSISTENT => true));
		    $this->db->query("set names utf8");
		    $this->db->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
		}catch (PDOException $e) {
			die("Connection failed: ".$e->getMessage( ));
			exit();
		}
    }
    
    function getDb(){
    	return  $this->db;
    }
    
    //寮�惎浜嬬墿
    function beginTransaction(){
    	//涓嶄富鍔ㄦ彁浜�
    	$this->db->setAttribute(PDO::ATTR_AUTOCOMMIT,0);
    	$this->db->beginTransaction();
    }
    
    //鎻愪氦
    function commit(){
    	$this->db->commit();
    	//涓诲姩鎻愪氦
    	$this->db->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
    }
    
    //鍥炴粴
    function rollback(){
    	$this->db->rollback();
    	//涓诲姩鎻愪氦
    	$this->db->setAttribute(PDO::ATTR_AUTOCOMMIT,1);
    }
    
    function close() {
    	$this->db = null;
    }
}  

?>